package com.application.tourapp.databasehandler;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.application.tourapp.dal.TypeDAL;
import com.application.tourapp.model.Item;
import com.application.tourapp.model.Type;

public class TourItemDatabaseHandler extends TourAppDatabaseHandler{
	
	private Context context;
	
	public TourItemDatabaseHandler(Context context) { 
		super(context);
		this.context = context;
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		super.onCreate(db);
		
	}
	
	public void addTourItem(Item item) {
        SQLiteDatabase db = this.getWritableDatabase();
        boolean exist = existsTourItem(item); 
        if(exist == false){
		    ContentValues values = new ContentValues();
		    values.put(KEY_ID_TI, item.getId()); 
		    values.put(KEY_NAME_TI, item.getName()); 
		    values.put(KEY_DESCRIPTION_TI, item.getDescription());
		    values.put(KEY_EMAIL_TI, item.getEmail());
		    values.put(KEY_ADDRESS_TI, item.getAddress());
		    values.put(KEY_LAT_TI, item.getLatitude());
		    values.put(KEY_LONG_TI, item.getLongitude());
		    values.put(KEY_WEBSITE_TI, item.getWebsite());
		    if(item.getType() != null) {
		    	values.put(KEY_TYPE_TI, item.getType().getId());
		    } else {
		    	values.put(KEY_TYPE_TI, 0);
		    }
		    db.insert(TABLE_TI, null, values);
        } else {
        	updateTourItem(item);
        }
        db.close(); 
    }
    
    public Item getTourItem(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_TI, new String[] { KEY_ID_TI,
                KEY_NAME_TYPE, KEY_DESCRIPTION_TI }, KEY_ID_TI + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Item item = new Item();
        item.setId(Integer.parseInt(cursor.getString(0)));
        item.setName(cursor.getString(1));
        item.setDescription(cursor.getString(2));
        return item;
    }
    
    public boolean existsTourItem(Item item) {
    	
    	SQLiteDatabase db = this.getReadableDatabase();
    	
    	int id = item.getId();
    	
        Cursor cursor = db.query(TABLE_TI, new String[] { KEY_ID_TI,
                KEY_NAME_TI, KEY_DESCRIPTION_TI }, KEY_ID_TI + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor.moveToFirst()) {
        	return true;
        } else {
        	return false;
        }
    }
    
    public ArrayList<Item> getAllTourItemsByType(String typeIds) {
    	ArrayList<Item> tourItemsList = new ArrayList<Item>();
        // Select All Query
    	int typeId = Integer.parseInt(typeIds);
        String selectQuery = "SELECT  * FROM " + TABLE_TI 
        		+ " WHERE " + KEY_TYPE_TI + " = " + "'" + typeId + "'";
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Item item = new Item();
                item.setId(Integer.parseInt(cursor.getString(0)));
                item.setName(cursor.getString(1));
                item.setPrix(cursor.getString(2));
                item.setEmail(cursor.getString(4));
                item.setAddress(cursor.getString(5));
                item.setLongitude(cursor.getString(6));
                item.setLatitude(cursor.getString(7));
                Type type = new TypeDAL(context).getType(Integer.parseInt(cursor.getString(8)));
                item.setType(type);
                item.setWebsite(cursor.getString(9));
                item.setDescription(cursor.getString(10));
                // Adding contact to list
                tourItemsList.add(item);
            } while (cursor.moveToNext());
        }
        //System.out.println("ALL TOUR ITEMS "+getAllTourItems().size());
        return tourItemsList;
    }
    
    
    public ArrayList<Item> getAllTourItems() {
    	ArrayList<Item> tourItemsList = new ArrayList<Item>();

        String selectQuery = "SELECT  * FROM " + TABLE_TI ;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Item item = new Item();
                item.setId(Integer.parseInt(cursor.getString(0)));
                item.setName(cursor.getString(1));
                item.setPrix(cursor.getString(2));
                item.setEmail(cursor.getString(4));
                item.setAddress(cursor.getString(5));
                item.setLongitude(cursor.getString(6));
                item.setLatitude(cursor.getString(7));
                Type type = new TypeDAL(context).getType(Integer.parseInt(cursor.getString(8)));
                item.setType(type);
                item.setWebsite(cursor.getString(9));
                item.setDescription(cursor.getString(10));
                // Adding contact to list
                tourItemsList.add(item);
            } while (cursor.moveToNext());
        }

        return tourItemsList;
    }
    
    public void deleteAllTourItems() {
    	SQLiteDatabase db = this.getWritableDatabase();
    	db.delete(TABLE_TI, null, null);
    }
    
    public int updateTourItem(Item item) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME_TI, item.getName());
        values.put(KEY_NAME_TI, item.getName()); 
	    values.put(KEY_DESCRIPTION_TI, item.getDescription());
	    values.put(KEY_EMAIL_TI, item.getEmail());
	    values.put(KEY_ADDRESS_TI, item.getAddress());
	    values.put(KEY_LAT_TI, item.getLatitude());
	    values.put(KEY_LONG_TI, item.getLongitude());
	    values.put(KEY_WEBSITE_TI, item.getWebsite());
	    if(item.getType() != null) {
	    	values.put(KEY_TYPE_TI, item.getType().getId());
	    }
        // updating row
        return db.update(TABLE_TI, values, KEY_ID_TI + " = ?",
                new String[] { String.valueOf(item.getId()) });
    }

}
